package com.sskj.xyft.mine;

import com.sskj.base.data.User;
import com.sskj.base.utils.RxUtils;
import com.sskj.xyft.BasePresenter;
import com.sskj.xyft.api.Api;
import com.sskj.xyft.api.ApiObserver;
import com.sskj.xyft.mine.data.MineImgBean;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;


/**
 * @author Hey
 * Create at  2019/03/20
 */
class MinePresenter extends BasePresenter<MineFragment> {

    public void uploadImage(File file) {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        builder.addFormDataPart("file_pic", file.getName(), RequestBody.create(MediaType.parse("image/png"), file));
        Api.getInstance().uploadImage(builder.build())
                .compose(RxUtils.transform())
                .subscribe(new ApiObserver<MineImgBean>(this, true) {
                    @Override
                    public void onSuccess(MineImgBean data) {
                        uploadHead(data.getUrl());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    public void uploadHead(String url) {
        Api.getInstance().uploadHead(url)
                .compose(RxUtils.transform())
                .subscribe(new ApiObserver<String>(this, true) {
                    @Override
                    public void onSuccess(String data) {
                        mView.uploadSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }


    public void getAccountDetail(String account, boolean showLoading) {
        Api.getInstance().accountDetail()
                .compose(RxUtils.transform())
                .subscribe(new ApiObserver<User>(this, showLoading) {
                    @Override
                    public void onSuccess(User data) {
                        mView.setAccountInfo(data);
                        mView.finishRefresh();
                    }
                });
    }


}
